package cn.itsource.rpms.web.controller;

import cn.itsource.rpms.domain.Opt;
import cn.itsource.rpms.service.IOptService;
import cn.itsource.rpms.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/opt")
@CrossOrigin
public class OptController {
    @Autowired
    private IOptService service;

    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    public List<Opt> queryAll(){
        return service.queryAll();
    }
    
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Opt queryOne(@PathVariable Long id){//将地址栏的id作为参数来接收
        return service.queryOne(id);
    }
    @RequestMapping(value = "/add",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult add(@RequestBody Opt opt){//将接收过来的json数据转换成对应的实体类对象
        try {
            service.add(opt);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");//保存失败就返回不保存失败的ajaxresult
        }
        return new AjaxResult();//成功就使用默认的
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id){
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");
        }
        return new AjaxResult();
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Opt opt){
        try {
            service.update(opt);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }
}
